package com.qf.controller;

import com.qf.model.Project;
import com.qf.page.PageResult;
import com.qf.page.QueryPageBean;
import com.qf.page.Result;
import com.qf.service.ProjectService;
import org.apache.commons.lang3.ObjectUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("project")
public class ProjectController {
    @Autowired
    private ProjectService projectService;
    @GetMapping("findAll")
    public List<Project> findAll(){
        List<Project> projects = projectService.getAll();
        return projects;
    }

    @PostMapping("all")
    public PageResult all(@RequestBody QueryPageBean queryPageBean){
            PageResult result =  projectService.findPage(queryPageBean);
        return result;
    }
    @GetMapping("findByPid")
    public Result findByPid(@RequestParam("pid") int pid){
        Project project = projectService.findByPid(pid);
        Result result = new Result();
        result.setMessage("fail");
        //commons-lang3  依赖
        //常用的工具类    StringUtils  ObjectUtils
        //
        // StringUtils.isBlank()
        //StringUtils.isEmpty()
        // if (project!=null)
        if (ObjectUtils.isNotEmpty(project)){
            result.setFlag(true);
            result.setData(project);
            result.setMessage("success");
        }
        return result;

    }


    @PostMapping("add")
    public Result addProject(@RequestBody Project project){
        int row = projectService.addProject(project);
//        Result result  = new Result();
        if (row>0){
//            result.setMessage("success");
//            result.setFlag(true);
            //枚举类名.对象名.get方法 获取到对应的属性值
//            result.setMessage(Status.SUCCESS.getMessage());
//            result.setFlag(Status.FAIL.isFlag());
            return Result.success();
        }else {
           return Result.fail();
        }

    }

    @PostMapping("edit")
    public Result edit(@RequestBody Project project){
        int row  =  projectService.edit(project);
        if (row>0){
//            result.setFlag(true);
//            result.setMessage("success");
            return Result.success();
        }else {
            return Result.fail();
        }
    }
    @GetMapping("delete")
    public Result delete(@RequestParam("pid") Integer pid){
        int row =  projectService.deleteByPid(pid);
        if (row>0){
            return Result.success();
        }else {
            return Result.fail();
        }
    }

}
